//
//  TDTextScrollController.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/13.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDTextScrollController.h"
#import "UIView+ZLExtension.h"
@interface TDTextScrollController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *redView;
@property(nonatomic,assign)CGFloat historyY;

@property(nonatomic,assign)CGFloat historyOffY;


@end

@implementation TDTextScrollController

- (void)viewDidLoad {
    [super viewDidLoad];
    _historyOffY = -60;
    self.tableView.delegate = self;
    self.tableView.dataSource  =self;
    _tableView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

#pragma mark - 实现数据源方法
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text  =@"测试的数据";
    if (indexPath.row == 0) {
        cell.textLabel.text  =@"测试的数据11111111";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //设置数据
    //返回
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_historyOffY<scrollView.contentOffset.y)
    {
        
//        if (scrollView.contentSize.height + scrollView.contentOffset.y ) {
//            <#statements#>
//        }
        NSLog(@"%.2f",scrollView.contentOffset.y);
        //表示是向上滑动
        //隐藏
        if (scrollView.zl_height + scrollView.contentOffset.y >= scrollView.contentSize.height || scrollView.contentOffset.y <= -60) {
            return;
        }
        CGFloat offsetY = scrollView.contentOffset.y - _historyOffY;
        if (_redView.zl_height > 0) {
            if (_redView.zl_height > offsetY) {
                _redView.zl_height = _redView.zl_height - offsetY;
            }else {
                _redView.zl_height = 0;
            }
        }
      
        //        [UIView animateWithDuration:0.25 animations:^{
        //            _redView.zl_height = 0;
        //        }];
    }
    else if(_historyOffY>scrollView.contentOffset.y)
    {
        NSLog(@"%.2f",scrollView.contentOffset.y);

        if (scrollView.zl_height + scrollView.contentOffset.y >= scrollView.contentSize.height || scrollView.contentOffset.y <= -60) {
            return;
        }
        //表示是向下面滑动
        CGFloat offsetY = scrollView.contentOffset.y - _historyOffY;
        //显示
        if (_redView.zl_height < 60) {
            _redView.zl_height = _redView.zl_height - offsetY;
        }else {
           _redView.zl_height = 60;
        }
//        [UIView animateWithDuration:0.25 animations:^{
//            _redView.zl_height = 60;
//        }];
        
    }
    _historyOffY=scrollView.contentOffset.y;
}

//区头跟随移动
//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    if (_historyY+20<targetContentOffset->y)
//    {
//        //表示是向上滑动
//        //隐藏
//        if (_redView.zl_height > 0) {
//
//        }
//        CGFloat offsetY = targetContentOffset->y - _historyY;
//        NSLog(@"%.2f",offsetY);
////        [UIView animateWithDuration:0.25 animations:^{
////            _redView.zl_height = 0;
////        }];
//    }
//    else if(_historyY-20>targetContentOffset->y)
//    {
//        //表示是向下面滑动
//        CGFloat offsetY = targetContentOffset->y - _historyY;
//        NSLog(@"%.2f",offsetY);
//        //显示
//        [UIView animateWithDuration:0.25 animations:^{
//            _redView.zl_height = 60;
//        }];
//    }
//    _historyY=targetContentOffset->y;
//}

@end
